import {CellData} from "./CellData";
import jsonTool from "../3rd/json";
import {Cell} from "exceljs";
import {V4MAPPED} from "dns";

export class TypeParse {
	public static CAN_NONE = true
	public static CheckNone(cell : CellData, default_val){
		if (this.CAN_NONE && cell.value == undefined || cell.value == null || cell.value == "") {
			cell.isEmpty = true
			cell.parseVal = default_val
			return default_val
		}
		cell.isEmpty = false
		// console.log(cell.value,cell.memberType)
		return cell.value
	}

	public static Parse_bool(cell : CellData){
		let oldv = TypeParse.CheckNone( cell, false)
		if (oldv == "1" || oldv == 1){
			cell.parseVal = true
		}else if (oldv == "0" || oldv == 0){
			cell.parseVal = false
		}
		cell.parseVal = TypeParse.CheckNone( cell, 0)
	}

	public static Parse_string(cell : CellData){
		cell.parseVal = TypeParse.CheckNone( cell, "")
	}

	public static Parse_int(cell : CellData){
		cell.parseVal = TypeParse.CheckNone( cell, 0)
	}

	public static ParseValue(cell : CellData) {
		let func = TypeParse[`Parse_${cell.memberType}`]
		if (func != null) {
			func(cell)
		} else {
			// console.log(cell.value, cell.memberType, cell.sheetName)
			cell.parseVal = jsonTool.parse(cell.value)
			cell.isEmpty = false
			if (cell.parseVal == undefined) {
				cell.isEmpty = true
				let listIndex = cell.memberType.indexOf("list")
				let mapIndex = cell.memberType.indexOf("map")
				if (listIndex == -1 && mapIndex == -1) {
					console.error("类型错误")
				} else if (listIndex == -1 && mapIndex != -1) {
					cell.parseVal = new Map()
				} else if (listIndex != -1 && mapIndex == -1) {
					cell.parseVal = []
				} else {
					if (listIndex > mapIndex) {
						cell.parseVal = new Map()
					} else {
						cell.parseVal = []
					}
				}
			}
		}
	}
}